// js传统实现栈
function Stack() {
    let items = [];

    this.push = function (element) {
        items.push(element)
    }
    this.pop = function () {
        return items.pop()
    }
    // peek:返回栈顶元素，不对栈做任何修改(这个方法不会溢出栈顶元素，仅仅返回它)
    this.peek = function () {
        return items[items.length-1]
    }
    // isEmpty:检查栈是否为空
    this.isEmpty = function () {
        return items.length == 0
    }
    // 清空
    this.clear = function () {
        items = []
    }
    // 打印
    this.print = function () {
        console.log(items.toString())
    }
}

let stack = new Stack();
//console.log(stack.isEmpty())


// ES6新特性实现栈
/*
class Stack{
    constructor(){
        this.items = [];
    }
    push(element){
        this.items.push(element)
    }
}
*/

// 十进制转二进制：十进制除2取余将余数依次push到栈中,十进制数为将余数依次出栈

function divideBy2(decNumber){
    let remStack = new Stack(),
        rem,
        binaryString = '';
    while (decNumber > 0){
        rem = Math.floor(decNumber % 2)
        remStack.push(rem)
        decNumber = Math.floor(decNumber / 2)
    }
    while (!remStack.isEmpty()){
        binaryString += remStack.pop().toString();
    }
    return binaryString
}

//console.log(divideBy2(10))

// 将十进制转换成其他进制

function baseConverter(decNumber,base) {
    var remStack = new Stack(),
    rem,
    baseString='',
    digits = '0123456789ABCDEF';
    while(decNumber >0){
        rem = Math.floor(decNumber % base);
        remStack.push(rem);
        decNumber = Math.floor(decNumber / base);
        console.log(rem,decNumber)
    }
    while(!remStack.isEmpty()){
        baseString += digits[remStack.pop()]
    }
    return baseString;
}

console.log(baseConverter(20,6))